package com.ruoyi.api;

import cn.dev33.satoken.stp.StpUtil;
import com.ruoyi.biz.domain.BizCourse;
import com.ruoyi.biz.domain.CourseRes;
import com.ruoyi.biz.mapper.BizClassRelevanceStudentMapper;
import com.ruoyi.biz.service.IBizCourseService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/course")
@Slf4j
public class ApiCourseController {

    @Resource
    private IBizCourseService courseService;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private BizClassRelevanceStudentMapper relevanceStudentMapper;

    /*
    * 查询当前登录用户的课程列表
    * */
    @GetMapping("/list")
    public AjaxResult getCourseList(){
        List<CourseRes> bizCourses = courseService.selectCourseList();

        return AjaxResult.success(bizCourses);
    }

    /*
    * 根据id查询课程信息
    * */
    @GetMapping
    public AjaxResult getCourseById(Long courseId){
        log.info("根据id:{}获取课程信息",courseId);
        BizCourse course = courseService.selectBizCourseById(courseId);

        // 根据课程获取到教师id，然后查询教师信息
        SysUser sysUser = sysUserMapper.selectUserById(course.getUserId());

        AjaxResult result = AjaxResult.success();

        result.put("course",course);
        result.put("teacher",sysUser);

        return result;
    }


    /*
    * 退出课程
    * */
    @GetMapping("/exit")
    public AjaxResult exitCourse(Long courseId){
        long loginId = StpUtil.getLoginIdAsLong();
        relevanceStudentMapper.exitCourse(courseId,loginId); // 退出课程

        return AjaxResult.success();
    }

}
